package saxion.speelveld.spaceengineer;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.controller.GameRowAdapter;
import saxion.speelveld.spaceengineer.database.GameDAO;
import saxion.speelveld.spaceengineer.model.Game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class LoadGamesScreen extends Activity
{

	private GameDAO gameDatasource;

	private ArrayList<Game> games = new ArrayList<Game>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_load_games_screen);

		ListView lvSaveGames = (ListView) findViewById(R.id.lvSaveGames);

		gameDatasource = new GameDAO(getApplicationContext());
		gameDatasource.open();
		games = gameDatasource.getAllGames(false, false, false, false);
		gameDatasource.close();

		GameRowAdapter adapter = new GameRowAdapter(this.getApplicationContext(), games);
		lvSaveGames.setAdapter(adapter);
		lvSaveGames.setOnItemClickListener(new LoadGameListener());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_load_games_screen, menu);
		return true;
	}

	class LoadGameListener implements OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			// TODO Auto-generated method stub
			Intent i = new Intent(getBaseContext(), GameScreen.class);
			i.putExtra("GameID", arg2 + 1);
			startActivity(i);

		}

	}

}
